package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.Mine;
import com.baosight.sczx.zm.cg.domain.ZMCGProduceStatis;

/**
 * 公司调度日报-原煤产量
 */
public class ServiceZMCG91 extends ServiceEPBase {
	
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);

		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdProjectName", "原煤产量", true);
		String fdVirtualDate = inInfo.getString("fdVirtualDate");
		if(fdVirtualDate == null || fdVirtualDate.trim().equals("")) {
			StaticTools.setQueryParameters(inInfo, "fdVirtualDate", StaticTools.getDate(), true);
		}
		else {
			StaticTools.setQueryParameters(inInfo, "fdVirtualDate", fdVirtualDate, true);
		}
		EiInfo outInfo = super.query(inInfo, "ZMCGProduceStatis.query", new ZMCGProduceStatis());
		//这里是公司的ID，获得的是矿的列表
        outInfo.addBlock(Mine.getMineBlock("cmes.cg.mine"));
        outInfo.addBlock(StaticBlocks.getBlockData("cmes.cg.status", new String[]{"草拟", "生效"}, new String[]{"草拟", "生效"}, true));//状态
		return outInfo;
	}	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGProduceStatis _dom = new ZMCGProduceStatis();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_PRODUCE_STATIS"));
			_dom.setFdVirtualDate(_dom.getFdCurrentDate());
			_dom.setFdProjectName("原煤产量");
			 
			boolean validate = validate(inInfo, _dom, "ZMCGProduceStatis.validate", null);
			if(validate) {
				out = this.query(inInfo);
				return out;
			}
			this.dao.insert("ZMCGProduceStatis.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGProduceStatis _dom = new ZMCGProduceStatis();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
		
			_dom.setFdVirtualDate(_dom.getFdCurrentDate());
			
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdKeyId", _dom.getFdKeyId());
			
			boolean validate = validate(inInfo, _dom, "ZMCGProduceStatis.validate", _map);
			if(validate) {
				out = this.query(inInfo);
				return out;
			}
			this.dao.update("ZMCGProduceStatis.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCGProduceStatis.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 生效</p> 
	 */
	public EiInfo report(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCGProduceStatis.report");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCGProduceStatis _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}

		params.put("fdReportDepartmentId", _dom.getFdReportDepartmentId());
		params.put("fdCurrentDate", _dom.getFdCurrentDate());
		params.put("fdProjectName", _dom.getFdProjectName());
		
		List result = dao.query("ZMCGProduceStatis.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每个单位每天只能填写一条记录！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	
	}
} 